package org.example.ding.api;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import static org.example.ding.api.RecommendationController.USER_SERVICE;

@RestController
@CrossOrigin(origins = "*")
public class LegacyRoutesController {

    @GetMapping("/api/nutrition-goals")
    public NutritionGoals getGoals(@RequestParam(name = "userId", required = false, defaultValue = "default") String userId) {
        RecommendationController.UserProfile u = USER_SERVICE.getOrCreate(userId);
        NutritionGoals g = new NutritionGoals();
        g.setUserId(userId);
        g.setCalorieGap(u.getCalorieGap());
        g.setProteinTargetRatio(u.getProteinTargetRatio());
        g.setProteinImportance(u.getProteinImportance());
        g.setLowCalImportance(u.getLowCalImportance());
        return g;
    }

    @PostMapping("/api/nutrition-goals")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateGoals(@RequestParam(name = "userId", required = false, defaultValue = "default") String userId,
                            @RequestBody NutritionGoals goals) {
        RecommendationController.UserProfile u = USER_SERVICE.getOrCreate(userId);
        if (goals.getCalorieGap() != null) u.setCalorieGap(Math.max(0, goals.getCalorieGap()));
        if (goals.getProteinTargetRatio() != null) u.setProteinTargetRatio(Math.max(0.0, Math.min(1.0, goals.getProteinTargetRatio())));
        if (goals.getProteinImportance() != null) u.setProteinImportance(Math.max(0.0, Math.min(1.0, goals.getProteinImportance())));
        if (goals.getLowCalImportance() != null) u.setLowCalImportance(Math.max(0.0, Math.min(1.0, goals.getLowCalImportance())));
    }

    public static class NutritionGoals {
        private String userId;
        private Integer calorieGap;
        private Double proteinTargetRatio;
        private Double proteinImportance;
        private Double lowCalImportance;

        public String getUserId() { return userId; }
        public void setUserId(String userId) { this.userId = userId; }
        public Integer getCalorieGap() { return calorieGap; }
        public void setCalorieGap(Integer calorieGap) { this.calorieGap = calorieGap; }
        public Double getProteinTargetRatio() { return proteinTargetRatio; }
        public void setProteinTargetRatio(Double proteinTargetRatio) { this.proteinTargetRatio = proteinTargetRatio; }
        public Double getProteinImportance() { return proteinImportance; }
        public void setProteinImportance(Double proteinImportance) { this.proteinImportance = proteinImportance; }
        public Double getLowCalImportance() { return lowCalImportance; }
        public void setLowCalImportance(Double lowCalImportance) { this.lowCalImportance = lowCalImportance; }
    }
}
